package main;

import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.List;

public class Calculator {

	public String divide(String a, String b, Integer... precision) {
		int aInt = Integer.parseInt(a);
		int bInt = Integer.parseInt(b);
		int p = (precision.length == 0) ? 5 : precision[0];
		if (bInt == 0)
			throw new DivideByZeroException("You can't  divide by zero!");
		String string = encodeToString(countSubstraction(changeNumber(aInt),
				changeNumber(bInt), p));
		if (((aInt != changeNumber(aInt) || bInt != changeNumber(bInt)) && !(aInt != changeNumber(aInt) && bInt != changeNumber(bInt)))) {
			return string = "-" + string;
		}
		return string;

	}

	public int changeNumber(int a) {
		if (a < 0) {
			a = a * -1;
		}
		return a;
	}

	public List<Integer> countSubstraction(int a, int b, int precision) {
		ArrayList<Integer> ar = new ArrayList<Integer>();
		int whole = 0;
		int rest = a;
		while ((rest != 0) && (precision != (ar.size() - 1))) {
			while (rest >= b) {
				rest = rest - b;
				whole++;
			}
			ar.add(whole);
			whole = 0;
			rest = rest * 10;
		}
		return ar;
	}

	public String encodeToString(List<Integer> ar) {
		String s = "";
		for (int i = 0; i < ar.size(); i++) {
			s = s + ar.get(i);
			if (i == 0 && ar.size() > 1)
				s = s + ".";
		}
		return s;
	}

	public double add(String aString, String bString) {
		double a = Double.parseDouble(aString);
		double b = Double.parseDouble(bString);
		a = a + b;
		return a;
	}

	public double sub(String aString, String bString) {
		double a = Double.parseDouble(aString);
		double b = Double.parseDouble(bString);
		a = a - b;
		return a;
	}

	public double mult(String aString, String bString) {
		double a = Double.parseDouble(aString);
		double b = Double.parseDouble(bString);
		double pom = a;
		int count = 0;
		while (b - 1 != count) {
			count++;
			a = a + pom;

		}
		return a;
	}

	public String squerRoots(String a) {
		int b;
		b=retFirstNumber(a);
		double c=returnSecondNumber(a);
		return a;
	}
	
	public double p(String a){
		double d =pareStringToDouble(a);
		int n = findAproxiametli(retFirstNumber(a));
		System.out.println(d+" "+n);
		d=d/2*n;
		return d;
	}
	
	
	public double returnSecondNumber(String a){
		String s=a.substring(a.indexOf(".")+1,a.length());
		double d =Double.parseDouble("0."+s);
		return d;
	}
	public int retFirstNumber(String a){
		 int b = Integer.parseInt((String) a.subSequence(0, a.indexOf(".")));
		 return b;
	}

	public double pareStringToDouble(String number) {
		return Double.parseDouble(number);
	}

	public int findAproxiametli(int a) {
		int pom = 0;
		for (int j = 0; j < 30; j++) {
			pom=j*j;
			if(pom>a)return j-1; 
		}
		return 0;
	}

}
